using System;
using System.ComponentModel.Composition;
using EnvDTE;
using Microsoft.VisualStudio.Shell;

namespace HQLAddin.ConfigurationInspector
{
	public interface IBuildWatcher
	{
		event EventHandler BuildSucced;
	}

	[Export(typeof(IBuildWatcher))]
	[Serializable]
	internal class BuildWatcher : IBuildWatcher
	{
		[ImportingConstructor]
		public BuildWatcher() 
		    : this((DTE)Package.GetGlobalService(typeof(DTE)))
		{
		}

		public BuildWatcher(DTE dte)
		{
			dte.Events.BuildEvents.OnBuildDone += BuildEventsOnBuildDone;
		}

		//This doesnt work.
		//[ImportingConstructor]
		//public BuildWatcher([Import]DTE dte)
		//{
		//    dte.Events.BuildEvents.OnBuildDone += BuildEventsOnBuildDone;
		//}

		private void BuildEventsOnBuildDone(vsBuildScope scope, vsBuildAction action)
		{
			OnBuildSucced();
		}

		public virtual event EventHandler BuildSucced;

		private void OnBuildSucced()
		{
			var handler = BuildSucced;
			if (handler != null) handler(this, EventArgs.Empty);
		}
	}

}